package com.ruoyi.hotel.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.core.domain.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.ruoyi.hotel.domain.HmCheckin;
import com.ruoyi.hotel.mapper.HmCheckinMapper;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import com.ruoyi.hotel.domain.bo.HmServiceRequestBo;
import com.ruoyi.hotel.domain.vo.HmServiceRequestVo;
import com.ruoyi.hotel.domain.HmServiceRequest;
import com.ruoyi.hotel.mapper.HmServiceRequestMapper;
import com.ruoyi.hotel.service.IHmServiceRequestService;

import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 【请填写功能名称】Service业务层处理
 *
 * @author ruoyi
 * @date 2023-11-01
 */
@RequiredArgsConstructor
@Service
public class HmServiceRequestServiceImpl implements IHmServiceRequestService {

    private final HmServiceRequestMapper baseMapper;

    /**
     * 查询【请填写功能名称】
     */
    @Override
    public HmServiceRequestVo queryById(Long serviceRequestId){
        return baseMapper.selectVoById(serviceRequestId);
    }

    /**
     * 查询【请填写功能名称】列表
     */
    @Override
    public TableDataInfo<HmServiceRequestVo> queryPageList(HmServiceRequestBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<HmServiceRequest> lqw = buildQueryWrapper(bo);
        Page<HmServiceRequestVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询【请填写功能名称】列表
     */
    @Override
    public List<HmServiceRequestVo> queryList(HmServiceRequestBo bo) {
        LambdaQueryWrapper<HmServiceRequest> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<HmServiceRequest> buildQueryWrapper(HmServiceRequestBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<HmServiceRequest> lqw = Wrappers.lambdaQuery();
        lqw.eq(bo.getCheckinId() != null, HmServiceRequest::getCheckinId, bo.getCheckinId());
        lqw.eq(StringUtils.isNotBlank(bo.getServiceRequestContent()), HmServiceRequest::getServiceRequestContent, bo.getServiceRequestContent());
        lqw.eq(bo.getServiceRequestTime() != null, HmServiceRequest::getServiceRequestTime, bo.getServiceRequestTime());
        lqw.eq(bo.getIsDone() != null, HmServiceRequest::getIsDone, bo.getIsDone());
        lqw.eq(StringUtils.isNotBlank(bo.getIsDone()), HmServiceRequest::getIsDone, bo.getIsDone());
        return lqw;
    }

    /**
     * 新增【请填写功能名称】
     */
    @Override
    public Boolean insertByBo(HmServiceRequestBo bo) {
        HmServiceRequest add = BeanUtil.toBean(bo, HmServiceRequest.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setServiceRequestId(add.getServiceRequestId());
        }
        return flag;
    }

    /**
     * 修改【请填写功能名称】
     */
    @Override
    public Boolean updateByBo(HmServiceRequestBo bo) {
        HmServiceRequest update = BeanUtil.toBean(bo, HmServiceRequest.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(HmServiceRequest entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除【请填写功能名称】
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }

    @Override
    public List<HmServiceRequest> getListByCheckin(HmCheckin checkin){
        QueryWrapper<HmServiceRequest> qw=new QueryWrapper<>();
        qw.eq("checkin_id",checkin.getCheckinId());
        return baseMapper.selectList(qw);
    }

    @Override
    public List<HmServiceRequest> getWaitingServiceRequests(){
        QueryWrapper<HmServiceRequest> qw=new QueryWrapper<>();
        qw.eq("is_done","w");
        return baseMapper.selectList(qw);
    }


}
